package com.kgc.cgprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.cgapi.pojo.CGSQInfo;
import com.kgc.cgapi.pojo.CGSQPojo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.cgprovider.dao.CGSQDao;
import com.kgc.cgprovider.service.CGSQService;
import com.kgc.cgprovider.service.CGServ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CGSQServiceImpl implements CGSQService {
    @Autowired
    private CGSQDao cgsqDao;

    @Autowired
    private CGServ cgServ;

    private Logger logger = LoggerFactory.getLogger(CGSQServiceImpl.class);

    @Override
    public ResponseMsg getCGSQPojo(String code) {
        logger.info("getCGSQPojo code:{}", code);
        CGSQPojo cgsqPojo = cgsqDao.getCGSQPojo(code);
        ResponseMsg responseMsg = new ResponseMsg();

        responseMsg.setCode(200);
        responseMsg.setData(cgsqPojo);
        logger.info("getCGSQPojo responseMsg:{}", responseMsg +"code" + code);
        return responseMsg;
    }
    @Override
    public ResponseMsg getCGSQInfo(String code){
        logger.info("getCGSQInfo code:{}", code);
        List<CGSQInfo> cgsqInfoList = cgsqDao.getCGSQInfo(code);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(200);
        responseMsg.setData(cgsqInfoList);
        logger.info("getCGSQInfo responseMsg:{}", responseMsg +"code" + code);
        return responseMsg;
    }

    @Override
    public ResponseMsg getPage(Map<String, Object> map) {
        logger.info("getPage map"+ map);
        ResponseMsg responseMsg = new ResponseMsg();
        int pageNow = 0;
        int pageSize = 0;
        if(map.get("pageNum") instanceof String){
            pageNow =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNow = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNow,pageSize);
        List<CGSQPojo> list = cgsqDao.getCGSQPage(map);
        PageInfo<CGSQPojo> pageInfo = new PageInfo<>(list);
        responseMsg.setCode(200);
        responseMsg.setData(pageInfo);
        logger.info("getPage pageInfo"+ pageInfo+"map"+ map);
        return responseMsg;
    }

    @Override
    public ResponseMsg addCGSQ(Map<String, Object> map) {
        logger.info("addCGSQ map{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        String code ="CGSQ"+"-"+ cgServ.getCode("cg_acgsq");
        map.put("code",code);
        Date date = new Date();
        map.put("create_date",date);
        int result = cgsqDao.addCGSQ(map);
        if (result>0){
            responseMsg.setCode(200);
            responseMsg.setData(code);
        }else {
            responseMsg.setCode(500);
        }
        logger.info("addCGSQ responseMsg:{}",responseMsg +"param" + map);
        return responseMsg;
    }

    @Override
    public ResponseMsg addCGSQInfo(Map<String, Object> map) {
        logger.info("addCGSQInfo map{}", map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        ArrayList<Map<String, Object>> info =(ArrayList<Map<String, Object>>) map.get("list");
        for(Map<String, Object> arr: info){
            Date date = new Date();
            arr.put("create_date",date);
            int result = cgsqDao.addCGSQInfo(arr);
            if (result<=0){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        responseMsg.setMsg("success");
        logger.info("addCGSQInfo responseMsg:"+responseMsg +"map"+ map);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiCGSQ(Map<String, Object> map) {
        logger.info("modiCGSQ map"+ map);
        ResponseMsg responseMsg = new ResponseMsg();
        Date date = new Date();
        map.put("modi_date",date);
        int result = cgsqDao.modiCGSQ(map);
        if (result>0){
            responseMsg.setCode(200);
            responseMsg.setMsg("success");
            responseMsg.setData(map);
        }else {
            responseMsg.setCode(500);
        }
        logger.info("modiCGSQ responseMsg:"+responseMsg +"map"+ map);
        return responseMsg;
    }

    @Override
    public ResponseMsg modiCGSQInfo(Map<String, Object> map) {
        logger.info("addCGSQInfo map"+ map);
        ResponseMsg responseMsg = new ResponseMsg();
        Date date = new Date();
        String code = (String) map.get("code");
        responseMsg.setCode(500);

        List<CGSQInfo> list = cgsqDao.getCGSQInfo(code);
        ArrayList<Map<String, Object>> info =(ArrayList<Map<String, Object>>) map.get("list");

        for(Map<String, Object> arr: info){
            if (arr.get("major_code") == null || arr.get("major_code") == ""){
                arr.put("major_code",code);
                arr.put("modi_date",date);
                int result = cgsqDao.addCGSQInfo(arr);
                if (result<=0){
                    responseMsg.setCode(500);
                    break;
                }
            }
            for (int i = 0;i< list.size() ; i++){
                if ((Integer)arr.get("id") == list.get(i).getId()){
                    list.remove(i);
                    break;
                }
            }
            int result = cgsqDao.modiCGSQInfo(arr);
            if (result<=0){
                return responseMsg;
            }

        }
        for (CGSQInfo delInfo : list){
            boolean isDel = cgServ.delInfo("cg_acgsq_info",delInfo.getId());
            if (!isDel){
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        logger.info("addCGSQInfo responseMsg:"+responseMsg +"map"+ map);
        return responseMsg;
    }


}
